package com.sacc.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.power.common.model.CommonResult;
import com.sacc.mapper.ProductCartMapper;
import com.sacc.pojo.ProductCart;
import com.sacc.service.ProductCartService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 滚~韬
 * @date 2022/4/8 13:27
 */
@Service
public class ProductCartServiceImpl implements ProductCartService {
    @Autowired
    private ProductCartMapper productCartMapper;

    /**
     * 获取购物车中的产品id
     * @param OpenId
     * @return
     */
    @Override
    public List<Integer> Get(String OpenId) {
        return Arrays.asList((Integer[]) ConvertUtils.convert(productCartMapper.selectById(OpenId).getProductId().split(","), Integer.class));
    }

    /**
     * 删除用户的购物车信息
     * @param productCart
     * @return
     */
    @Override
    public CommonResult Delete(ProductCart productCart) {
       try{
           List<Integer> productIdDeleted = new ArrayList<>(Arrays.asList((Integer[]) ConvertUtils.convert(productCart.getProductId().split(","),Integer.class)));//被删除的productid
           ProductCart productCartHaved= productCartMapper.selectById(productCart.getOpenId());//拥有的boxCollect
           List<Integer> productIdHaved = new ArrayList<>(Arrays.asList((Integer[]) ConvertUtils.convert(productCartHaved.getProductId().split(","),Integer.class)));
           //如果删除的productid和之前的一样,默认全部删除,否则去除子集并且更新
           productIdDeleted.forEach(b->{
               if(productIdHaved.contains(b)){
                   productIdHaved.remove(b);
               }
           });
           if(productIdHaved.isEmpty()){
               productCartMapper.deleteById(productCartHaved.getOpenId());
               return new CommonResult("200","删除成功");
           }
           String collect = productIdHaved.stream().map(String::valueOf).collect(Collectors.joining(","));//将数字用逗号连接
           productCartHaved.setProductId(collect);
           productCartMapper.updateById(productCartHaved);
           return new CommonResult("200","删除成功");
       }catch (Exception e){
           return new CommonResult("400","删除失败"+e.toString());
       }
    }

    /**
     * 添加用户的购物车信息
     * @param productCart
     * @return
     */
    @Override
    public CommonResult Add(ProductCart productCart) {
        try {
            ProductCart cart = productCartMapper.selectById(productCart.getOpenId());
            if(cart!=null){
                //原来有的
                List<Integer> integerList1 = Arrays.asList((Integer[]) ConvertUtils.convert(cart.getProductId().split(","), Integer.class));
                //后面加的
                List<Integer> integerList2 = Arrays.asList((Integer[]) ConvertUtils.convert(productCart.getProductId().split(","), Integer.class));
                //转为ArrayList
                List<Integer> integers = new ArrayList<>(integerList2);
                integers.addAll(integerList1);
                List<Integer> accountIdList = integers.stream().distinct().collect(Collectors.toList());
                StringBuilder stringBuilder = new StringBuilder();
                accountIdList.forEach(a->{
                    stringBuilder.append(",").append(a);
                });
                String s = stringBuilder.toString().replaceFirst(",", "");
                cart.setProductId(s);
                productCartMapper.updateById(cart);
            }else {
                productCartMapper.insert(productCart);
            }
            return new CommonResult("200","添加成功");
        }catch (Exception e){
            return new CommonResult("400","添加失败"+e.toString());
        }
    }


}
